Control Structure

Common Lisp provides a variety of special structures for organizing programs. Some have to do with flow of control (control structures), while others control access to variables (environment structures). Some of these features are implemented as special forms; others are implemented as macros, which typically expand into complex program fragments expressed in terms of special forms or other macros.

Function application is the primary method for construction of Lisp programs. Operations are written as the application of a function to its arguments. Usually, Lisp programs are written as a large collection of small functions, each of which implements a simple operation. These functions operate by calling one another, and so larger operations are defined in terms of smaller ones. Lisp functions may call upon themselves recursively, either directly or indirectly.


#new4#

While the Lisp language is more applicative in style than statement-oriented, it nevertheless provides many operations that produce side effects and consequently requires constructs for controlling the sequencing of side effects. The construct <#9#>progn<#9#>, which is roughly equivalent to an Algol <#10#>begin<#10#>-<#11#>end<#11#> block with all its semicolons, executes a number of forms sequentially, discarding the values of all but the last. Many Lisp control constructs include sequencing implicitly, in which case they are said to provide an ``implicit <#12#>progn<#12#>.'' <#3354#>implicit <#13#>progn<#13#><#3354#> Other sequencing constructs include <#14#>prog1<#14#> and <#15#>prog2<#15#>.

For looping, Common Lisp provides the general iteration facility <#16#>do<#16#> as well as a variety of special-purpose iteration facilities for iterating or mapping over various data structures.

Common Lisp provides the simple one-way conditionals <#17#>when<#17#> and <#18#>unless<#18#>, the simple two-way conditional <#19#>if<#19#>, and the more general multi-way conditionals such as <#20#>cond<#20#> and <#21#>case<#21#>. The choice of which form to use in any particular situation is a matter of taste and style.

Constructs for performing non-local exits with various scoping disciplines are provided: <#22#>block<#22#>, <#23#>return<#23#>, <#24#>return-from<#24#>, <#25#>catch<#25#>, and <#26#>throw<#26#>.

The multiple-value constructs provide an efficient way for a function to return more than one value; see <#27#>values<#27#>.